package ink.yql.service.Impl;

import ink.yql.dao.TeamUserDao;
import ink.yql.model.TeamUser;
import ink.yql.model.User;
import ink.yql.service.TeamUserService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional
public class TeamUserServiceImpl implements TeamUserService {
    @Autowired
    private TeamUserDao teamUserDao;
    @Autowired
    private HttpServletRequest req;

    @Override
    public RestObject getByTid(Integer tid) {
        if (tid == null){
            return RestObject.builder().code("400").message("请输入正确的数据").build();
        }
        List<TeamUser> list = teamUserDao.getByTid(tid);
        return RestObject.builder().code("200").message("查询团队的所有队员成功").data(list).build();
    }

    @Override
    public RestObject getByUid() {
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            return RestObject.builder().code("404").message("请先登录").build();
        }
        List<TeamUser> list = teamUserDao.getByUid(user.getUid());
        return RestObject.builder().code("200").message("查询登录用户的所有团队成功").data(list).build();
    }

    @Override
    public RestObject addOne(TeamUser teamUser) {
        try{
            Integer one = teamUserDao.addOne(teamUser);
            return RestObject.builder().code("200").message("团队添加队员成功").build();
        }catch (Exception e){
            return RestObject.builder().code("200").message("该队员已经在团队中了,不能重复添加").build();
        }
    }

    @Override
    public RestObject delete(TeamUser teamUser) {
        try{
            Integer one = teamUserDao.delete(teamUser);
            return RestObject.builder().code("200").message("团队添加队员成功").build();
        }catch (Exception e){
            return RestObject.builder().code("200").message("该队员已经在团队中了,不能重复添加").build();
        }
    }
}
